package com.jdqzyy.noobnoob.common.resultVO;

import com.jdqzyy.noobnoob.common.constant.response.ResponseEnum;
import org.springframework.http.HttpStatus;

import java.io.Serializable;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author Zyy
 * @Description: 用于处理接口返回，统一返回的格式
 * @date 2020/11/21下午2:24
 * @slogan 天下风云出我辈，一入代码岁月催
 */
public class NoobResult extends LinkedHashMap<String,Object> implements Serializable {
    private static final long serialVersionUID = 1L;
    private static final String MSG = "message";
    private static final String CODE = "code";
    private static final String DATA = "data";
    private static final String TOTAL = "total" ;

    /**
     * 默认是返回正确
     */
    public NoobResult () {
        this.exchange(HttpStatus.OK);
    }

    /**
     * @return 处理正确，返回正确代码
     */
    public static NoobResult ok() {
        NoobResult resultOk = new NoobResult();
        return resultOk;
    }
    /**
     * 手动存放httpStatus类型的code与msg
     * @param httpStatus
     * @return
     */
    public static NoobResult ok(HttpStatus httpStatus) {
        NoobResult resultOk = new NoobResult();
        resultOk.exchange(httpStatus);
        return resultOk;
    }
    /**
     * 手动存放ResultCodeMsg类型的code与msg
     * @param result
     * @return
     */
    public static NoobResult ok(ResponseEnum result) {
        NoobResult resultOk = new NoobResult();
        resultOk.exchange(result);
        return resultOk;
    }
    /********处理错误的返回**********/
    /**
     * @return 处理正确，返回错误代码
     */
    public static NoobResult error() {
        NoobResult resultError = new NoobResult();
        resultError.exchange(HttpStatus.INTERNAL_SERVER_ERROR);
        return resultError;
    }
    /**
     * 手动存放ResultCodeMsg类型的code与msg
     * @param httpStatus
     * @return
     */
    public static NoobResult error(HttpStatus httpStatus) {
        NoobResult resultError = new NoobResult();
        resultError.exchange(httpStatus);
        return resultError;
    }
    /**
     * 手动存放ResultCodeMsg类型的code与msg
     * @param result
     * @return
     */
    public static NoobResult error(ResponseEnum result) {
        NoobResult resultError = new NoobResult();
        resultError.exchange(result);
        return resultError;
    }
    /**
     * 添加data，用于存放需要返回的数据
     * @param data 数据
     * @return
     */
    public NoobResult data(Object data) {
        put(DATA,data);
        return this;
    }
    /**
     * 添加total，用于存放返回的数据列表中，数据量
     * @Param total 数据总量
     */
    public NoobResult total(Integer total) {
        put(TOTAL,total);
        return this;
    }
    /**
     * 更改msg
     * @param msg
     * @return
     */
    public NoobResult msg(String msg) {
        put(MSG,msg);
        return this;
    }
    /**
     * 自动定一个返回map，然后放到result中
     * @param map
     * @return
     */
    public NoobResult putMap(Map map) {
        this.putAll(map);
        return this;
    }
    /**
     * 将自定义的内容存放进入返回中
     */
    @Override
    public NoobResult put(String key, Object value) {
        super.put(key, value);
        return this;
    }
    /**
     * 将HttpStatus或者ResultCodeMsg枚举类按照格式放入Response类中
     */
    private void exchange (Object enumClass) {
        if (enumClass instanceof HttpStatus) {
            HttpStatus httpStatus = (HttpStatus) enumClass;
            put(CODE, httpStatus.value());
            put(MSG, httpStatus.getReasonPhrase());
        }
        else if (enumClass instanceof ResponseEnum){
            ResponseEnum resultCodeMsg = (ResponseEnum) enumClass;
            put(CODE, resultCodeMsg.code());
            put(MSG, resultCodeMsg.msg());
        }
    }
}
